/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.model.Enhancement;
import org.crystalrange.service.EnhancementService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zkplus.databind.AnnotateDataBinder;

public class EnhancementController extends BaseController {

    protected EnhancementService enhancementService;
    protected Enhancement enhancement;

    public EnhancementService getEnhancementService() {
        return this.enhancementService;
    }

    public void setEnhancementService(EnhancementService enhancementService) {
        this.enhancementService = enhancementService;
    }

    public Enhancement getEnhancement() {
        return this.enhancement;
    }

    public void setEnhancement(Enhancement enhancement) {
        this.enhancement = enhancement;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.enhancement = this.enhancementService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Enhancement");
        } else {
            this.enhancement = this.enhancementService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Enhancement");
        }

        component.setAttribute("controller", this, true);
    }

    public void onCreate$windowEnhancement(Event event) throws Exception {
        this.binder = (AnnotateDataBinder) this.self.getAttribute("binder");
    }

    public void onClick$menuitemEnhancementSave(Event event) throws Exception {
        this.enhancementSave();
    }

    public void onClick$toolbarbuttonEnhancementSave(Event event) throws Exception {
        this.enhancementSave();
    }

    private void enhancementSave() throws Exception {
        this.enhancementService.save(this.enhancement,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
